<?php

require_once './db_env.php';

function exec_sql($sql)
{
    $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWD, DB_NAME, DB_PORT);
    if ($con->connect_error) {
        // die("连接失败: " . $con->connect_error);
        return "连接失败: " . $con->connect_error;
    }
    $con->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
    $result = $con->query($sql);
    if ($result) {
        // 输出数据
        if ($result->num_rows > 0) {
            $output_data = array();
            while ($row = $result->fetch_assoc()) {
                array_push($output_data, $row);
            }
            return $output_data;
        } else if (is_bool($result)) {
            return $result;
        }
        return false;
    } else {
        return false;
    }
    // $con->close();
}

function exec_sql_array($sql_arr)
{
    $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWD, DB_NAME, DB_PORT);
    if ($con->connect_error) {
        // die("连接失败: " . $con->connect_error);
        return "连接失败: " . $con->connect_error;
    }
    $con->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
    $results = array();
    foreach ($sql_arr as $sql) {
        $result = $con->query($sql . ';');
        array_push($results, $result);
    }
    // $con->close();
    return $results;
}
